package goodbaby.dklt.fragment;

import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.classic.core.utils.NumberTextAnimation;
import com.classic.core.utils.NumberUtils;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import goodbaby.dklt.R;
import goodbaby.dklt.activity.AttendanceActivity;
import goodbaby.dklt.activity.LoginActivity;
import goodbaby.dklt.bean.BodyInfo;
import goodbaby.dklt.customerview.Top5ListView;

/**
 * Created by Li.z on 2016/4/29.
 * 身高，体重...
 */
public class BodyInfoFragment extends HealthBaseFragment {
    private static final String TAG = "BodyInfoFragment";
    private TextView tvBMINumber;
    private TextView tvBMIInfo;
    private TextView tvFat;
    private TextView tvHeight;
    private TextView tvWeight;

    private Top5ListView listview;

    public static boolean shouldLoadData = true;

    @Override
    public int getLayoutResId() {
        return R.layout.fragment_body_info;
    }

    @Override
    public void initView(View parentView) {
        super.initView(parentView);

        tvBMINumber = (TextView) parentView.findViewById(R.id.tv_bmi_number);
        tvBMIInfo = (TextView) parentView.findViewById(R.id.tv_bmi_info);
        tvFat = (TextView) parentView.findViewById(R.id.tv_fat_number);
        tvHeight = (TextView) parentView.findViewById(R.id.tv_height_number);
        tvWeight = (TextView) parentView.findViewById(R.id.tv_weight_number);

        listview = (Top5ListView) parentView.findViewById(R.id.tlv_top_list);
    }

    public void loadData() {
        ActionHelp.getBodyInfo(getActivity(), new ObjectCallback<BodyInfo>() {

            @Override
            public void onError(int code) {
                if (code == 301) {
                    Intent intent = new Intent(getActivity(), LoginActivity.class);
                    startActivity(intent);
                } else {
                    ToastUtil.showToast(activity, "失败:" + code);
                }
            }

            @Override
            public void onSuccess(BodyInfo response) {
                Log.i(TAG, response.toString());
                showView(response);
            }

            @Override
            public Type getType() {
                return new TypeToken<BodyInfo>() {
                }.getType();
            }

            @Override
            public void onError(int code, String errorMessage) {
                super.onError(code, errorMessage);
                ToastUtil.showToast(getActivity(), "请求错误");
            }

            @Override
            public void onHttpError(int statusCode) {
                super.onHttpError(statusCode);
                ToastUtil.showToast(getActivity(), "请求超时");
            }
        });
    }

    private void showView(BodyInfo info) {
        BodyInfo.ChildrenBody childrenBody = info.getChildrenbody();
        if (childrenBody != null) {
            NumberTextAnimation.runFloat(tvBMINumber, NumberUtils.str2Float(childrenBody.getBMI_VAL()), 2);
            NumberTextAnimation.runFloat(tvFat, NumberUtils.str2Float(childrenBody.getBFR()), 1);
            NumberTextAnimation.runFloat(tvHeight, NumberUtils.str2Float(childrenBody.getHeight()), 0, "%scm");
            NumberTextAnimation.runFloat(tvWeight, NumberUtils.str2Float(childrenBody.getWeight()), 0, "%skg");

            tvBMIInfo.setText(childrenBody.getBMI_Name());
        }

        showTop5List(info.getBMITop5List());
    }


    private void showTop5List(final List<BodyInfo.Top5User> list) {
        listview.setTitle("BMI排行榜");
        listview.setUsernameColor("#fefefe");
        listview.setNumberColor("#fefefe");
        listview.setPositionColor("#fff701");
        listview.setTitleBackground(R.mipmap.health_bang_title2);

        if (list == null) {
            return;
        }

        listview.setDatas(new Top5ListView.DataAdapter() {
            @Override
            public List<Top5ListView.Top5Data> getData() {
                BodyInfo.Top5User tempUser;
                List<Top5ListView.Top5Data> datas = new ArrayList<Top5ListView.Top5Data>(list.size());
                for (int i = 0; i < list.size() && i < 5; i++) {
                    tempUser = list.get(i);
                    datas.add(new Top5ListView.Top5Data(tempUser.getImagePath(), tempUser.getName(), tempUser.getBMI_VAL() + ""));
                }
                return datas;
            }
        });


    }

    /*@Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        if(isVisibleToUser && shouldLoadData){
            shouldLoadData = false;
            loadData();
        }
    }*/

}
